import { login, wxlogin, logout, getInfo, reg, pwdchange } from "@/api/login";
import { getNext, binduser, postChkCode } from "@/api/user";
import { getToken, setToken, removeToken } from "@/utils/auth";

const user = {
  state: {
    token: getToken(),
    name: "",
    avatar: "",
    roles: [],
    userInfo: [],
    chkcode: ""
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token;
    },
    SET_NAME: (state, name) => {
      state.name = name;
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar;
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles;
    },
    SET_USERINFO: (state, info) => {
      state.userInfo = info;
    },
    SET_CHKCODE: (state, code) => {
      state.chkcode = code;
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim();
      return new Promise((resolve, reject) => {
        login(username, userInfo.password)
          .then(response => {
            const data = response.data;
            setToken(data.token);
            commit("SET_TOKEN", data.token);
            resolve(data);
          })
          .catch(error => {
            reject(error);
          });
      });
    },

    wxLogin({ commit }, info) {
      return new Promise((resolve, reject) => {
        wxlogin(info.openid, info.pid)
          .then(response => {
            const data = response.data;
            setToken(data.token);
            commit("SET_TOKEN", data.token);
            resolve(data);
          })
          .catch(error => {
            reject(error);
          });
      });
    },

    bindUser({ commit }, info) {
      return new Promise((resolve, reject) => {
        binduser(info)
          .then(response => {
            const data = response.data;
            setToken(data.token);
            commit("SET_TOKEN", data.token);
            resolve(data);
          })
          .catch(error => {
            reject(error);
          });
      });
    },

    //处理注册信息
    Reg({ commit }, userInfo) {
      //console.log(userInfo)
      const username = userInfo.username.trim();
      return new Promise((resolve, reject) => {
        reg(
          username,
          userInfo.nickname,
          userInfo.password,
          userInfo.repassword,
          userInfo.pid
        )
          .then(response => {
            const data = response.data;
            setToken(data.token);
            commit("SET_TOKEN", data.token);
            resolve(data);
          })
          .catch(error => {
            reject(error);
          });
      });
    },

    //修改密码
    Pwdchange({ commit }, formpw) {
      return new Promise((resolve, reject) => {
        pwdchange(formpw.oldpw, formpw.password, formpw.repassword)
          .then(response => {
            resolve(response.data);
          })
          .catch(error => {
            reject(error);
          });
      });
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo(state.token)
          .then(response => {
            const data = response.data;
            console.log(data, 111111111111111);
            if (data.roles && data.roles.length > 0) {
              // 验证返回的roles是否是一个非空数组
              commit("SET_ROLES", data.roles);
            } else {
              reject("getInfo: roles must be a non-null array !");
            }
            commit("SET_NAME", data.name);
            commit("SET_AVATAR", data.avatar);
            commit("SET_USERINFO", data);
            resolve(response);
          })
          .catch(error => {
            reject(error);
          });
      });
    },

    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token)
          .then(() => {
            commit("SET_TOKEN", "");
            commit("SET_ROLES", []);
            removeToken();
            resolve();
          })
          .catch(error => {
            reject(error);
          });
      });
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise((resolve, reject) => {
        commit("SET_TOKEN", "");
        removeToken();
        resolve();
      });
    },

    puser({ commit }) {
      return new Promise((resolve, reject) => {
        getNext()
          .then(response => {
            resolve(response.data);
          })
          .catch(er => reject(er));
      });
    },
    //发送验证码
    PostChkCode({ commit }, tel) {
      return new Promise((resolve, reject) => {
        postChkCode(tel.toString().trim())
          .then(response => {
            console.log(response);

            switch (response) {
              case "-1002":
                //showError($("#msgCode"),"短信发送失败，请重试");
                alert("短信发送失败，请重试");
                resolve(false);
                break;
              case "-1001":
                //showError($("#msgCode"),"验证码获取频繁，请一分钟后再试");
                alert("验证码获取频繁，请一分钟后再试");
                resolve(false);
                break;
            }

            commit("SET_CHKCODE", response);
            resolve(true);
          })
          .catch(error => {
            console.log(error);
            alert("发送失败，请重试");
            reject(false);
          });
      });
    }
  }
};

export default user;
